package com.jinyi.wwdeng.ui.user.impl

import android.annotation.SuppressLint
import com.jinyi.base.mvp.BaseModel
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.base.utils.log.HHLog
import com.jinyi.lib_http.callback.StringCallback
import com.jinyi.lib_http.model.Progress
import com.jinyi.lib_http.model.Response
import com.jinyi.lib_http.request.base.Request
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.ui.user.contract.UserFeedbackContract
import org.json.JSONObject

/**
 * JinYiTest
 *
 * @Author：wwdeng
 * @CreateTime：2024/4/14 18:42
 * <pre>
 * Model层  接口实现类
</pre> *
 */
class UserFeedbackModelImpl(iCallBack: UserFeedbackContract.Presenter) : BaseModel<UserFeedbackContract.Presenter>(iCallBack), UserFeedbackContract.Model {
    /**
     * 获取反馈问题类型列表
     */
    override fun loadQuestionTypeListAction() {
        val dataList = arrayListOf(
            "Product Suggestion,SUGGEST",
            "App bugs,BUG",
            "Payment issues,PAYMENT",
            "Others,OTHER")
        val typeList = arrayListOf<JSONObject>()
        for (i in 0..<dataList.size){
            val items = dataList[i].split(",")
            val obj = JSONObject()
            obj.put("title", items[0])
            obj.put("value", items[1])
            obj.put("isSelect",false)
            typeList.add(obj)
        }
        callBack.setQuestionTypeListData(typeList)
    }

    /**
     * 获取联系方式列表
     */
    override fun loadContactListAction() {
        val data = JSONObject(AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_COMMON))
        val contactList = arrayListOf<JSONObject>()
        val customize = data.optJSONObject("customize")
        if (customize != null){
            val mobileList = customize.optString("mobile").split(",")
            for (i in mobileList.indices){
                val obj = JSONObject()
                obj.put("type","MOBILE")
                obj.put("value", mobileList[i])
                contactList.add(obj)
            }
            val emailList = customize.optString("email").split(",")
            for (i in emailList.indices){
                val obj = JSONObject()
                obj.put("type","EMAIL")
                obj.put("value", emailList[i])
                contactList.add(obj)
            }
        }
        callBack.setContactListData(contactList)
    }

    //上传图片
    override fun uploadImageAction(imgPath: String){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_UPLOAD_FILE_URL)
        NetworkUtils.uploadFile(httpUrl,imgPath,object : StringCallback(){
            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                HHLog.e("正在上传中...")
            }
            override fun onSuccess(response: Response<String>?) {
                if (response!!.code() == 200){
                    callBack.setUploadImageSuccess(JSONObject(response.body()))
                } else {
                    callBack.onRequestError(response!!.message())
                }
            }

            override fun onError(response: Response<String>?) {
                HHLog.e("上传出错"+response!!.body())
                val data = JSONObject(response.body())
                callBack.onRequestError(data.optString("msg"))
            }

            override fun uploadProgress(progress: Progress) {
            }

            override fun onFinish() {
                callBack.onRequestFinish()
            }
        })
    }

    /**
     * 提交反馈
     */
    override fun submitFeedbackAction(params: JSONObject) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onSubmitFeedbackSuccess(data)
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onRequestError(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
                callBack.onRequestFinish()
            }
        })
    }
}
